package cn.stard.org.controller;

import cn.stard.basic.annotation.PreAuthorize;
import cn.stard.basic.util.LoginContext;
import cn.stard.org.query.DeptQuery;
import cn.stard.basic.util.AjaxResult;
import cn.stard.basic.util.PageList;
import cn.stard.org.domain.DePartment;
import cn.stard.org.service.IDepartmentservice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*复合注解拥有两个的所有功能*/
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private IDepartmentservice service;


        /*
        * @Title:
        * @Description: 通过id 查询数据
        * @Author: Mr.She
        * @Version: 1.0
        * @Date:
        * @Parameters:
        * @Return
        */

    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询")
    @PreAuthorize(name="通过id查找字段", sn = "department:findeDePartment")
    public DePartment findeDePartment(@PathVariable("id")Long id){
        DePartment dePartment = service.loadById(id);

        return dePartment;
    }
    // 分页查询@RequestBody 请求获取参数
    @PostMapping
    @PreAuthorize(name="分页查询", sn = "department:list")
    public PageList<DePartment> list(@RequestBody DeptQuery deptQuery, HttpServletRequest req){
        Object loginUser = LoginContext.getLogininfo(req);
        System.out.println("看监听到数据没有"+loginUser);
        System.out.println("进入高查");
        PageList<DePartment> data = service.queryData(deptQuery);

        return data;
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name="删除部门", sn = "department:dele1")
    public AjaxResult dele1(@PathVariable("id")Long id){
        try {
            System.out.println("执行1");
            service.remove(id);
            return  AjaxResult.me();
        }catch (Exception e){
            System.out.println("执行2");
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }


    }


    @PatchMapping
    @PreAuthorize(name="批量删除", sn = "department:deles")
    public AjaxResult deles(@RequestBody List<Long> ids){
        try {

            service.deles(ids);
            return  AjaxResult.me();
        }catch (Exception e){
            System.out.println("执行2");
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }


    }




    @PutMapping
    @PreAuthorize(name="添加修改", sn = "department:addorupt")
    public AjaxResult addorupt(@RequestBody  DePartment dePartment){
        try {
            if (dePartment.getId()==null){
                System.out.println(dePartment);
                service.save(dePartment);

            }else {
                service.update(dePartment);
            }
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }

    }

    @GetMapping("/tree")
  /*  @PreAuthorize(name="无限级树", sn = "department:tree")*/
    public List<DePartment> tree(){
        System.out.println("进入方法001");
        return  service.tree();
    }

    @GetMapping// 请求地址：http://localhost:8080/employee GET请求
    @PreAuthorize(name="展示列表", sn = "department:loadAll")
    public List<DePartment> loadAll(){
        return service.loadAll();
    }
}
